<template>
  <div class="addConsultant-page">
    <GhNavBar title="咨询师管理-添加" />
    <div class="consultant-content">
     
        <van-field  v-model="consultantInfo.avatar"   label="图片链接" required label-width="60px" autocomplete="off" ></van-field>
        <van-field  v-model="consultantInfo.name"   label="姓名" required label-width="60px" autocomplete="off" ></van-field>
        <van-field v-model="consultantInfo.coun_label"  name="radio"   label="等级" required label-width="80px" autocomplete="off" >
             <template #input> 
                <van-radio-group v-model="consultantInfo.coun_label"  direction="horizontal">
                    <van-radio name="1">1星</van-radio>
                    <van-radio name="2">2星</van-radio>
                    <van-radio name="3">3星</van-radio>
                  </van-radio-group>
             </template>
        </van-field>
        <van-field
            v-model="consultantInfo.introduction"
            rows="5"
            required
            class="custom-field"
            label="详情"
            label-width="40px"
            autosize
            maxlength="250"
            type="textarea"
            show-word-limit
            placeholder="咨询师简介"
          />
        <van-field  v-model="consultantInfo.experience"   label="工作经验" required label-width="70px" autocomplete="off" type="digit" placeholder="请输入整数" ></van-field>
        <van-field  v-model="consultantInfo.account"   label="联系方式" required label-width="70px" autocomplete="off" placeholder="请输入标准手机号码"></van-field>
        <van-field  v-model="consultantInfo.gender" name="radio"    label="性别" required label-width="80px" autocomplete="off" >
            <template #input> 
               <van-radio-group v-model="consultantInfo.gender" direction="horizontal">
                   <van-radio name="男">男</van-radio>
                   <van-radio name="女">女</van-radio>
                 </van-radio-group>
            </template>
       </van-field>
        <van-button native-type="submit"  :loading="isRegistering" :disabled="isRegistering" @click="submit_consultant" block round type="primary">
            确定添加
          </van-button>

    </div>
  </div>
</template>

<script setup>
import {ref,reactive,onMounted} from "vue"
import { Toast, Dialog } from 'vant';
import 'vant/es/toast/style'
import {addNotice} from '@/services/admin'
import {useRouter} from 'vue-router'
const router=useRouter();
import {addConsultant} from '@/services/admin'



//定义咨询师信息响应式变量
const consultantInfo =ref({
    avatar:'',
    name:'',
    coun_label:'2',
    introduction:'',
    experience:'',
    account:'',
    gender:'男'
})

const isRegistering = ref(false);
//添加咨询师功能
const submit_consultant =()=>{
    if(consultantInfo.value.avatar.trim()===''){
        Toast('请输入图片链接')
        return
    }
    if(consultantInfo.value.name.trim()===''){
        Toast('请输入姓名')
        return
    }
    if(consultantInfo.value.introduction.trim()===''){
        Toast('请输入简介')
        return
    }
    if(consultantInfo.value.experience.trim()===''){
        Toast('请输入工作经验')
        return
    }
    if(consultantInfo.value.account.trim()===''){
        Toast('请输入手机号码')
        return
    }
    isRegistering.value = true;
    const formData=new FormData();
    formData.append('avatar',consultantInfo.value.avatar)
    formData.append('name',consultantInfo.value.name)
    formData.append('coun_label',consultantInfo.value.coun_label)
    formData.append('introduction',consultantInfo.value.introduction)
    formData.append('experience',consultantInfo.value.experience)
    formData.append('account',consultantInfo.value.account)
    formData.append('gender',consultantInfo.value.gender)
    setTimeout(async()=>{
      const res=await addConsultant(formData).then((response)=>{
      return response
    }).catch((error)=>{
     return error
    })
   console.log(res)
   Toast.success('添加成功！')
   router.push('/admin_consultants')
   isRegistering.value=false
   },1000)
}



</script>

<style lang="scss" scoped>
.addConsultant-page{
    width: 100%;
      .consultant-content{
        margin-top: 40px;
        width: 100%;

        .van-button{
        margin-top: 20px;
        width: 80%;
        left: 50%;
        transform: translate(-50%);
       }
      }


}

</style>
